#!coding=utf-8
'''

@author: sglnd
'''
import base64
import hashlib

import requests  # @UnresolvedImport
import json
from django.http.response import HttpResponse


def getURL(billno):
    respdata=[];
    data="['%s']" % billno;
    data_digest=base64.encodestring((hashlib.md5(u"%s%s"%(data,"80E574A99881B673ED7C20A529E56D29")).digest())).strip('\n')  # @UndefinedVariable
    url="http://japi.zto.cn/zto/api_utf8/traceInterface?data=['{0}']&data_digest={1}&msg_type=NEW_TRACES&company_id=0b60240597ea4d76916ed6287a4fabae&billCodes={2}".format(billno,data_digest,billno);
    print url;
    data=requests.get(url);
    res=json.loads(data.content);
    resdata=res['data'][0];
    for i in resdata:
        if i == 'traces':
            for j in range(len(resdata[i])):
                strtmp= resdata[i][j]['scanDate']+"+";
                
                if (u'收件' in resdata[i][j]['desc'] or u'到达' in resdata[i][j]['desc'] or u'派件' in resdata[i][j]['desc'] or u'签收' in resdata[i][j]['desc']):
                    strtmp+= resdata[i][j]['desc'];
                else:
                    strtmp+= resdata[i][j]['desc']+",目的地："+resdata[i][j]['preOrNextSite'];
#                 print resdata[i][j]['dispOrRecMan'];
#                 print resdata[i][j]['dispOrRecManPhone'];
                respdata.append(strtmp);
                print strtmp
    print respdata;
    encodedjson = json.dumps(respdata);
    return HttpResponse(encodedjson);
getURL('400990348993')
